/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bulkmailer.db;

/**
 *
 * @author ravi
 */

import java.sql.Connection;
import java.sql.DriverManager;
import org.apache.log4j.Logger;

public class DBConnector {

    public static String SCHEMA_NAME = "BulkMailer";
    static Connection con = null;
    static int connectionCount = 0;

    private DBConnector() {
    }

    public static Connection getConnection(String dbSchema) throws Exception {
        try {
            if (con == null) {
                connectionCount++;
                Logger.getRootLogger().debug("created connection: " + connectionCount);
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager.getConnection("jdbc:mysql:///" + dbSchema,
                        "root", "root");
                if (!con.isClosed()) {
                    System.out.println("Connected to MySQL server using TCP/IP...");
                }
            } else {
                Logger.getRootLogger().debug("old connection returned");
            }
        } catch (Exception e) {
            Logger.getRootLogger().error(e.toString());
            throw e;
        }
        return con;
    }

    public void closeConnection(Connection con) throws Exception {
        con.close();
    }
}
